﻿using System;

namespace Homework4
{
    class Time
    {
        private int second;
        private int hour;
        private int minute;
        public void next()
        {
            if (second < 59)
                second++;
            else if (minute < 59)
            {
                second = 0;
                minute++;
            }
            else if (hour < 23)
            {
                minute = 0;
                hour++;
            }
            else
                hour = 0;
        }
        public int get()
        {
            return hour * 10000 + minute * 100 + second;
        }
        public void set(int h, int m, int s)
        {
            hour = h;
            minute = m;
            second = s;
        }
    }
    class Clock
    {
        public delegate void Tick();

        public event Tick tick1;
        public delegate void Alarm();
        public event Alarm alarm1;
        private Time now = new Time();
        private Time ring_time = new Time();

        public void adjust_now(int h, int m, int s)
        {
            now.set(h, m, s);
            Console.WriteLine($"当前时间是：{h}时，{m}分，{s}秒");

        }
        //设定闹铃时间
        public void adjust_ring(int h, int m, int s)
        {
            ring_time.set(h, m, s);
            Console.WriteLine($"闹钟设定时间是：{h}时，{m}分，{s}秒");

        }
        //时间过一秒，就判断一下有没有到闹钟设定的时间。
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("时间过了1秒！"); //实现tick的功能
            now.next(); 
            if (now.get() >= ring_time.get())
            {
                //时间到了就执行alarm1事件。
                alarm1();
                
            }
        }
        public void tick_start()
        {
            if (tick1 != null)
            {
                tick1(); /* tick1事件被触发 */
            }
            else
            {
                Console.WriteLine("event not fire");
                //Console.ReadKey(); /* 回车继续 */
            }
        }
        public void alarm()
        {
            Console.WriteLine("时间到了！");
        }
        public void tick()
        {
            //实现一个1秒钟的计时器
            var timer = new System.Timers.Timer();
            timer.Elapsed += timer_Elapsed;
            timer.AutoReset = true; //会一直执行timer_Elapsed
            timer.Enabled = true;
            timer.Interval = 1000; //1秒的时间间隔
            Console.Read();
            
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Clock c = new Clock();
            c.adjust_now(18, 35, 40);     //起始时间
            c.adjust_ring(18, 35, 43);    //闹铃时间
            c.tick1 += new Clock.Tick(c.tick);
            c.alarm1 += new Clock.Alarm(c.alarm);
            c.tick_start();
            
        }
    }
}
